#! /bin/sh -vx
# Public domain.

BinDir=${BinDir:-..}
ExeExt=${ExeExt:-}
_mpost=$BinDir/mpost$ExeExt
_pltotf=$BinDir/pltotf$ExeExt

# pre-generated test results in the repository are stored in LF
# but the output might be written in CRLF on some platform.
# if 'diff --strip-trailing-cr' is available, exploit it.
# (useful for tests on win32 binaries run on MSYS shell)
DIFF="diff"
$DIFF --strip-trailing-cr $0 $0 \
  && DIFF="diff --strip-trailing-cr" || echo

testdir=$abs_srcdir/triptrap

TEXMFCNF=$testdir; export TEXMFCNF

rm -rf mptrapdir
mkdir mptrapdir
cd mptrapdir

dvitype_args="-output-level=2 -dpi=72.27 -page-start='*.*.*.*.*.*.*.*.*.*'"

echo ">>> Running MPTRAP test for MetaPost." >&2
echo ">>> See $testdir/mptrap.diffs for example of acceptable diffs." >&2

set -x

$_pltotf $testdir/trapf.pl trapf.tfm

# get same filename in log
$LN_S $testdir/mtrap.mp . || exit 1

$_mpost --ini mtrap
diff $testdir/mtrap.log mtrap.log
$DIFF $testdir/mtrap.0 mtrap.0
$DIFF $testdir/mtrap.1 mtrap.1
$DIFF $testdir/writeo writeo
$DIFF $testdir/writeo.2 writeo.2

$LN_S $testdir/trap.mp . || exit 1
$LN_S $testdir/trap.mpx . || exit 1

## Why is --jobname required? And why --ini?
$_mpost --ini --jobname=trap <$testdir/mptrap1.in >mptrapin.fot
mv trap.log mptrapin.log || exit 1
diff $testdir/mptrapin.log mptrapin.log

exit 0

